package com.intmall.flink.operator;

import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

public class SinkToFileTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        DataStreamSource<Event> stream = env.fromElements(
                new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L),
                new Event("Alice", "./cart", 2100L),
                new Event("Bob", "./product?id=1", 3000L),
                new Event("Bob", "./product?id=2", 3500L),
                new Event("Bob", "./product?id=3", 4000L),
                new Event("Bob", "./product?id=3", 3600L),
                new Event("Alice", "./product?id=1", 5100L),
                new Event("Mary", "./center", 6000L)
        );

        StreamingFileSink<String> fileSink = StreamingFileSink.<String>forRowFormat(new Path("./output"), new SimpleStringEncoder<>("UTF-8"))
                .withRollingPolicy(DefaultRollingPolicy
                        .builder()
                        .withMaxPartSize(1024 * 1024 * 1024)
                        .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                        // 最近5分钟没有收到新的数据，滚动分区文件
                        .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                        .build())
                .build();

        stream.map(Event:: toString).addSink(fileSink);

        env.execute();
    }
}
